package comNast

import (
	"fmt"
	"github.com/nats-io/nats.go"
)

type SelfNast struct {
	NC *nats.Conn
}

func NewSelfNast(url string) (*SelfNast, error) {
	connect, err := nats.Connect(
		url,
	)
	if err != nil {
		fmt.Println(err)
		return nil, err
	}
	fmt.Println("Connected to NATS server")

	return &SelfNast{connect}, nil
}

func (l *SelfNast) Publish(str string, data []byte) {
	go func() {
		err := l.NC.Publish(str, data)
		if err != nil {
			fmt.Println(err)
		}
	}()

}
func (l *SelfNast) Subscribe(subject string, fnc nats.MsgHandler) {
	go func() {
		_, err := l.NC.Subscribe(subject, fnc)
		if err != nil {
			fmt.Println(err)
		}
		select {}
	}()
}
